let express=require("express")();
let expressStatic=require("express-static");
let mysql=require("mysql");
let port=81;
let sql=mysql.createConnection({
	host:'localhost',
	user:'root',
	password:'123456',
	database:'student',
	timezone:'08:00'
});
console.log(port)
sql.connect();
//操作数据库
express.get("/getstudent",(request,response)=>{
	sql.query("SELECT * FROM student",(error,data)=>{
		if(error){
			console.log(error);
			response.send("error");
		}else{
			console.log(data);
			response.send(data);
		}
	})
})
//删除功能
express.get("/deletestudent",(request,response)=>{
	sql.query(`DELETE FROM student WHERE id=${request.query.id}`,(error,data)=>{
		if(error){
			console.log(error);
			response.send("error");
		}else{
			response.send("success");
		}
	})
})
//添加学生功能
express.get("/addstudent",(request,response)=>{
	sql.query(`INSERT INTO student (name,age,sex,city,data) VALUES("${request.query.name}","${request.query.age}","${request.query.sex}","${request.query.city}","${request.query.data}")`,(error,data)=>{
		if(error){
			console.log(error);
			response.send("error");
		}else{
			response.send("success");
		}
	})
})
//实现编辑功能
express.get("/writestudent",(request,response)=>{
	sql.query(`UPDATE student SET name="${request.query.name}",age="${request.query.age}",sex="${request.query.sex}",city="${request.query.city}",data="${request.query.data}" WHERE id="${request.query.id}"`,(error,data)=>{
		if(error){
			console.log(error);
			response.send("error");
		}else{
			response.send("success");
		}
	})
})
//加入静态页面
express.use(expressStatic(__dirname+"/text"));
//启动服务
express.listen(port);